import QtQuick
import QtQuick.Controls
import QtQuick.Layouts

Rectangle {
    id: windowToolBar
    color: "transparent"
    border.color: "#d0d0d0"
    radius: 8
    border.width: 0
    height: 32
    
    // 属性
    property var mainWindow: null
    property bool isMaximized: false
    property string title: ""
    
    // 窗口拖拽区域
    MouseArea {
        anchors.fill: parent
        property point startMousePos
        property point startWindowPos
        property real deltaX: 0
        property real deltaY: 0
        property bool isDragging: false

        onPressed: {
            startMousePos = Qt.point(mousePosition.globalX, mousePosition.globalY);
            deltaX = mousePosition.globalX - windowToolBar.mainWindow.x;
            deltaY = mousePosition.globalY - windowToolBar.mainWindow.y;
            isDragging = true;
            dragTimer.start();
        }

        onReleased: {
            isDragging = false;
            dragTimer.stop();
        }

        onPositionChanged: {
            if (pressed) {
                // 实时更新窗口位置
                if (windowToolBar.mainWindow) {
                    windowToolBar.mainWindow.x = mousePosition.globalX - deltaX;
                    windowToolBar.mainWindow.y = mousePosition.globalY - deltaY;
                }
            }
        }

        // 定时器用于平滑拖拽，避免卡顿
        Timer {
            id: dragTimer
            interval: 16  // 约60FPS
            repeat: true
            running: false
            
            onTriggered: {
                if (parent.isDragging && parent.pressed && windowToolBar.mainWindow) {
                    // 使用全局鼠标坐标
                    windowToolBar.mainWindow.x = mousePosition.globalX - parent.deltaX;
                    windowToolBar.mainWindow.y = mousePosition.globalY - parent.deltaY;
                }
            }
        }
    }

    // 标题显示（居中，绝对定位）
    Text {
        anchors.horizontalCenter: parent.horizontalCenter
        anchors.verticalCenter: parent.verticalCenter
        text: windowToolBar.title
        font.pixelSize: 16
        font.bold: true
        color: "#333333"
        visible: windowToolBar.title !== ""
    }
    
    RowLayout {
        anchors.fill: parent
        anchors.margins: 4
        spacing: 2
        
        Item {
            Layout.fillWidth: true
        }
        
        // 最小化按钮
        Rectangle {
            width: 24
            height: 24
            color: minimizeMouseArea.containsMouse ? "#e0e0e0" : "transparent"
            border.color: "#d0d0d0"
            border.width: 1
            radius: 2
            
            Text {
                anchors.centerIn: parent
                text: "−"
                font.pixelSize: 16
                font.bold: true
                color: "#333333"
            }
            
            MouseArea {
                id: minimizeMouseArea
                anchors.fill: parent
                hoverEnabled: true
                propagateComposedEvents: true
                onClicked: {
                    if (windowToolBar.mainWindow) {
                        windowToolBar.mainWindow.showMinimized()
                    }
                }
            }
        }
        
        // 最大化/还原按钮
        Rectangle {
            width: 24
            height: 24
            color: maximizeMouseArea.containsMouse ? "#e0e0e0" : "transparent"
            border.color: "#d0d0d0"
            border.width: 1
            radius: 2
            
            Text {
                anchors.centerIn: parent
                text: windowToolBar.isMaximized ? "⧉" : "□"
                font.pixelSize: 14
                font.bold: true
                color: "#333333"
            }
            
            MouseArea {
                id: maximizeMouseArea
                anchors.fill: parent
                hoverEnabled: true
                propagateComposedEvents: true
                onClicked: {
                    if (windowToolBar.mainWindow) {
                        if (windowToolBar.isMaximized) {
                            windowToolBar.mainWindow.showNormal()
                            windowToolBar.isMaximized = false
                        } else {
                            windowToolBar.mainWindow.showMaximized()
                            windowToolBar.isMaximized = true
                        }
                    }
                }
            }
        }
        
        // 关闭按钮
        Rectangle {
            width: 24
            height: 24
            color: closeMouseArea.containsMouse ? "#e81123" : "transparent"
            border.color: "#d0d0d0"
            border.width: 1
            radius: 2
            
            Text {
                anchors.centerIn: parent
                text: "×"
                font.pixelSize: 16
                font.bold: true
                color: closeMouseArea.containsMouse ? "#ffffff" : "#333333"
            }
            
            MouseArea {
                id: closeMouseArea
                anchors.fill: parent
                hoverEnabled: true
                propagateComposedEvents: true
                onClicked: {
                    if (windowToolBar.mainWindow) {
                        // 可以在这里添加未保存内容的检查逻辑
                        windowToolBar.mainWindow.close()
                    }
                }
            }
        }
    }
}